package control;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.GestionScores;
import model.Joueur;
import model.Partie;

/**
 * Servlet implementation class PlayControl
 */
@WebServlet(name="/StartControl",urlPatterns="/StartControl.jsp")

public class StartControl extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    private Partie laPartie;   
  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StartControl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		
		if(action != null){
			
			if(action.equals("start")){
				boolean np;
				System.out.println("NEw Partie");
				
				String nom = request.getParameter("nom");
				if(nom == null){
					String re = request.getParameter("replay");
					if(re!= null){
						
						this.laPartie = new Partie(new Joueur(this.laPartie.getMonJoueur().getNom(),this.laPartie.getMonJoueur().getLevel()));
						//this.laPartie.setMotATrouver("accuss");
						HttpSession hs = request.getSession();
						hs.setAttribute("partie", this.laPartie);
					}
					
					
				}else{
					int level = Integer.parseInt(request.getParameter("level"));
					
					this.laPartie = new Partie(new Joueur(nom, level));
					
					//this.laPartie.setMotATrouver("accuss");
					HttpSession hs = request.getSession();
					hs.setAttribute("partie", this.laPartie);	
					
					
				}
	
			}// Si l'action est de jouer
			else if(action.equals("play")){
				String lettre = request.getParameter("lettre");
				if(lettre != null){
					System.out.println("La lettre testé est : "+lettre);
					request.setAttribute("lettre",lettre);
					String g = (String)request.getAttribute("lettre");
					if(g != null){
				    char c  = g.toLowerCase().charAt(0);
					if(laPartie.getNbCoup() < 9 && laPartie.isFin() == false && laPartie.lettreJoue(c)==false)
			    	{
						laPartie.getLettreDejaJouer().add(c);
				    	if(laPartie.estDansleMot(c)){
					    			
					    			ArrayList<Integer> list = laPartie.aQuelEndroit(c);
					    			for(Integer p : list){
					    				laPartie.getMap().put(p,new Character(c));
					    			}
					    		
					    		}else{
					    			laPartie.setNbCoup(laPartie.getNbCoup()+1);
					    		}
					    		
					    	
					    	
				   }
					if(laPartie.win(laPartie.getMap())){
					   System.out.println("create fileproper");
					   	GestionScores gs = new GestionScores();
						Properties props = new Properties();
						String coup = String.valueOf(laPartie.getNbCoup());
						props.setProperty("joseph",coup);
						try {
							gs.saveProperties(props);
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
				   }
					
				    	
					    	
					    		//request.setAttribute("map",map);
					    	
				    	
				    }
					
				}
			}else if(action.equals("score")){
				//GestionScores gs = new GestionScores();
				
			}
			RequestDispatcher dis = request.getRequestDispatcher("/game.jsp");
			try {
				dis.forward(request,response);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
